<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>体佛法师讲义内容搜索</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            max-width: 800px;
            margin: 2rem auto;
            padding: 0 20px;
            background-color: #f5f5f5;
        }
        h1 {
            color: #2c3e50;
            text-align: center;
            margin-bottom: 2rem;
        }
        form {
            background: white;
            padding: 2rem;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        input[type="text"] {
            width: 100%;
            padding: 12px;
            margin: 8px 0;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
            font-size: 16px;
        }
        button {
            background-color: #3498db;
            color: white;
            padding: 12px 24px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s;
        }
        button:hover {
            background-color: #2980b9;
        }
        #response {
            margin-top: 2rem;
            background: white;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            padding: 20px;
            min-height: 100px;
        }
        h2 {
            color: #2c3e50;
            margin-top: 2rem;
        }
    </style>
    <script>
        async function submitQuery(event) {
            event.preventDefault();
            const query = document.getElementById('query').value;
            const responseDiv = document.getElementById('response');
            responseDiv.textContent = ''; // Clear previous response

            const response = await fetch('/query', {
                method: 'POST',
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                body: new URLSearchParams({ query })
            });

            const reader = response.body.getReader();
            const decoder = new TextDecoder();
            while (true) {
                const { done, value } = await reader.read();
                if (done) break;
                responseDiv.textContent += decoder.decode(value); // Append streamed tokens
            }
        }
    </script>
</head>
<body>
    <h1>南无阿弥陀佛</h1>
    <form onsubmit="submitQuery(event)">
        <input type="text" id="query" name="query" required><br><br>
        <button type="submit">云何</button>
    </form>
    <h2>如是我闻:</h2>
    <div id="response" style="white-space: pre-wrap; border: 1px solid #ccc; padding: 10px;"></div>
</body>
</html>
